LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2010, 02:56 AM   #1
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Rep: Reputation: 15
[awk] NR problem


I have 2 cases. If I did
Code:
 awk '{if(NR==27)$2="NaN";print NR"\t"$2}' input > output
I can get result. It means for NR=27, the second column in output file will be NaN

But I modified the command
Code:
for nn in 27 
do
awk '{if(NR=="'"${nn}"'")$2="NaN";print NR"\t"$2}' input > output
done
I didn't get the result the same as the first way. It means there is no change for the second column of NR=27.

Are there some tricks to deal with it?
Any kind help is greatly thanked.
 
Old 08-12-2010, 03:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well not sure why you are doing the for loop outside the awk seeing it has one of its own, but if we stay with this format, try setting an awk variable:
Code:
for nn in 27 
do
    awk -vn=$nn '{if(NR==n)$2="NaN";print NR"\t"$2}' input > output
done
Also I would submit an alternative to the awk, but of course each to their own:
Code:
for nn in 27 
do
    awk -vn=$nn 'NR == n{$2="NaN"}1' input > output
done
This of course takes out your test print
 
Old 08-12-2010, 10:03 PM   #3
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
I must replace randomly some NR with NaN.
I tried your code, but there is no change.
Below is my complete code
Code:
for nn in 27 134 181 191 194 196
do
    awk -vn=$nn 'NR == n{$2="NaN"}1' input > output
done
 
Old 08-13-2010, 12:43 AM   #4
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
I think my title is no so clear. I move my problem to new thread.
Sorry for this inconvenience.
 
Old 08-13-2010, 04:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please mark this one as SOLVED then.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Awk Problem - Help Me kalyanofb General 1 02-01-2007 05:12 AM
A problem about awk jackk294 Programming 4 07-13-2006 08:56 PM
awk problem alaios Programming 4 05-04-2003 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration